package com.chenshu.shop.controller.admin;

import com.chenshu.shop.model.Type;
import com.chenshu.shop.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created with IntelliJ IDEA
 * Description
 * User：chenshu
 * Date：2024-06-06
 * Time：09:09
 */
@Controller
@RequestMapping("/admin")
public class TypeController {
    @Autowired
    TypeService typeService;
    /**
     * 获取所有类型
     * @param request
     * @return
     */
    @RequestMapping("/typeList")
    public String getAllTypes(HttpServletRequest request) {
        List<Type> typeList = typeService.getAllTypes();
        request.setAttribute("flag", 1);
        request.setAttribute("typeList", typeList);
        return "forward:/admin/type_list.jsp";
    }

    /**
     * 删除分类
     * @param id
     * @return
     */
    @RequestMapping("/typeDelete")
    public String deleteType(HttpServletRequest request, Integer id) {
        List<Type> types = typeService.deleteType(id);
        request.setAttribute("typeList", types);
        return "forward:/admin/type_list.jsp";
    }

    /**
     * 重定向到类目添加页
     * @return
     */
    @RequestMapping("/type_add")
    public String addType() {
        return "redirect:/admin/type_add.jsp";
    }

    /**
     * 添加类目
     * @param request
     * @param name
     * @param num
     * @return
     */
    @RequestMapping("/typeSave")
    public String saveType(HttpServletRequest request,String name, Integer num) {
        request.setAttribute("flag", 1);
        List<Type> types = typeService.saveType(name, num);
        request.setAttribute("typeList", types);
        return "forward:/admin/type_list.jsp";
    }

    /**
     * 将需要编辑的类目的id传递并且重定向到type_edit.jsp
     * @param request
     * @param id
     * @return
     */
    @RequestMapping("/typeEdit")
    public String editType(HttpServletRequest request, Integer id) {
        request.setAttribute("flag", 1);
        Type type = typeService.getTypeById(id);
        request.setAttribute("type", type);
        request.setAttribute("msg", "您正在修改类目信息");
        return "forward:/admin/type_edit.jsp";
    }

    /**
     * 修改类目
     * @param request

     * @return
     */
    @RequestMapping("/typeUpdate")
    public String updateType(HttpServletRequest request, Type type) {
        request.setAttribute("flag", 1);
        List<Type> types = typeService.updateType(type);
        request.setAttribute("typeList", types);
        return "forward:/admin/type_list.jsp";
    }
}
